package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.CheckJoinUsRecommendAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.CompanyAPI;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.EventBusEntity;
import com.runen.infinitemovement.model.entity.TopCompanyRecommendEntity;
import com.runen.infinitemovement.widget.popupwind.InputTenantPassWordPopupWindow;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class CheckJoinUsRecommendActivity extends BaseActivity {
    @BindView(R.id.rv_recommend)
    RecyclerView rvRecommend;
    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_more_team)
    TextView tvMoreTeam;
    @BindView(R.id.ll_add)
    LinearLayout llAdd;
    @BindView(R.id.et_input_search)
    TextView edtInputSearch;

    private CheckJoinUsRecommendAdapter checkJoinUsRecommendAdapter;
    private int changeBranch;
    private String edtString = "";
    private InputTenantPassWordPopupWindow popupWindow;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_join_us_recommend;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("选择加入组织");
        changeBranch = getIntent().getIntExtra("changeBranch", 0);
        topBtnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        edtInputSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CheckJoinUsRecommendActivity.this, CheckJoinUsActivity.class);
                intent.putExtra("changeBranch", changeBranch);
                intent.putExtra("isShowSoftKeyboard", true);
                startActivity(intent);
            }
        });
//        edtInputSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//修改回车键功能
//                    //代码
//                    edtString = v.getText().toString().trim();
//                    initData();
//                }
//                return false;
//            }
//        });
        llAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CheckJoinUsRecommendActivity.this, CreateLevel1OrganizationsActivity.class));
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvRecommend.setLayoutManager(layoutManager);
        checkJoinUsRecommendAdapter = new CheckJoinUsRecommendAdapter(this, new CheckJoinUsRecommendAdapter.OnClick() {
            @Override
            public void join(String companyId, boolean isNeedPassWord) {
                if (isNeedPassWord) {
                    popupWindow = InputTenantPassWordPopupWindow.getInstance(CheckJoinUsRecommendActivity.this,
                            new InputTenantPassWordPopupWindow.InputContent() {
                                @Override
                                public void inputContent(String password) {
                                    verifyPassword(companyId, password);
                                }
                            });
                    popupWindow.show();
                } else {
                    jumpToCheckTeamActivity(companyId);
                }
            }
        });
        rvRecommend.setAdapter(checkJoinUsRecommendAdapter);

        tvMoreTeam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CheckJoinUsRecommendActivity.this, CheckJoinUsActivity.class);
                intent.putExtra("changeBranch", changeBranch);
                startActivity(intent);
            }
        });
    }

    private void jumpToCheckTeamActivity(String companyId) {
        Intent intent = new Intent(CheckJoinUsRecommendActivity.this, CheckTeamActivity.class);
        if (changeBranch == 1) {
            intent.putExtra("type", 1);
        }
        intent.putExtra("companyId", companyId);
        startActivity(intent);
    }

    /**
     * 验证密码是否正确
     *
     * @param companyId
     * @param password
     */
    private void verifyPassword(String companyId, String password) {
        showDialog();
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", companyId);
            object.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new CompanyAPI().postVerifyPassword(object.toString(), new BaseResultCallback<BaseResponseEntity>() {
            @Override
            public void onResponse(BaseResponseEntity response) {
                dismissDialog();
                if (response != null && response.getCode() == 0) {
                    jumpToCheckTeamActivity(companyId);
                    if (popupWindow != null && popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        showDialog();
        new UserAPI().getCompanyRecommend(new BaseResultCallback<TopCompanyRecommendEntity>() {
            @Override
            public void onResponse(TopCompanyRecommendEntity response) {
                dismissDialog();
                if (response.code == 0) {
                    if (response.data != null) {
                        if (response.data.size() > 0) {
                            checkJoinUsRecommendAdapter.setNewInstance(response.data);
                            checkJoinUsRecommendAdapter.notifyDataSetChanged();
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }

            }

            @Override
            public void onError(Throwable e, String body) {
                super.onError(e, body);
                dismissDialog();
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBusEntity event) {
        switch (event.getMsg()) {
            case 4:
                finish();
                break;
            default:
                break;
        }
    }
}
